package com.qzsoft.zhjd.home.v2.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.qzsoft.zhjd.home.v2.dto.AsphaltDataWeightDto;
import com.qzsoft.zhjd.home.v2.dto.ProductionCountDto;
import com.qzsoft.zhjd.home.v2.service.ZhjdHomeV2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.home.v2.dto.EnvDataDto;
import com.qzsoft.zhjd.home.v2.dto.MaterielCountDto;
import com.qzsoft.zhjd.home.v2.dto.ProductionMonthCountDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;

/**
 * @ClassName ZhjdHomeV2Controller
 * @Description 养护基地首页控制器
 * @author linhuawei
 * @since 2022-08-17
 */
@RestController
@RequestMapping("/home/v2")
public class ZhjdHomeV2Controller {

    @Autowired
    private ZhjdHomeV2Service zhjdHomeV2Service;

    /**
     * @Title productionCount
     * @Description 产量统计
     * @param baseCode 基地主键
     * @return 当前基地产量统计
     */
    @GetMapping("/production/count")
    public Result<List<ProductionCountDto>> productionCount(String baseCode) {
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx("基地编号不可为空");
        }
        List<ProductionCountDto> list = zhjdHomeV2Service.productionCount(baseCode);
        return ResultUtils.ok(list);
    }

    /**
     * @Title productionMonthCount
     * @Description 月度产量统计
     * @param type     统计类型：asphalt沥青，cement水稳
     * @param baseCode 基地编号
     * @param year     年份
     * @return 月度产量统计
     */
    @GetMapping("/production/monthCount")
    public Result<List<ProductionMonthCountDto>> productionMonthCount(String type, String baseCode, Integer year) {
        // 参数校验
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx("基地编号不可为空");
        }
        if (ObjectUtils.isEmpty(year)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            year = calendar.get(Calendar.YEAR);
        }

        // 获取数据
        List<ProductionMonthCountDto> list = null;
        if (ObjectUtils.isEmpty(type) || !"cement".equals(type)) {
            list = zhjdHomeV2Service.productionAsphaltCount(baseCode, year);
        } else {
            list = zhjdHomeV2Service.productionCementCount(baseCode, year);
        }
        return ResultUtils.ok(list);
    }

    /**
     * @Title productionRealTime
     * @Description 生产实时情况
     * @param params 查询条件
     * @return 生产实时情况
     */
    @GetMapping("/production/realTime")
    public Result<List<AsphaltDataWeightDto>> productionRealTime(@RequestParam Map<String, Object> params) {
        List<AsphaltDataWeightDto> data = zhjdHomeV2Service.productionRealTime(params);
        return ResultUtils.ok(data);
    }

    /**
     * @Title materielCount
     * @Description 收发料统计
     * @param weightType 统计类型：send发料，recive收料
     * @param baseCode   基地编号
     * @param type       时间：year本年，month本月，day本日
     * @param time       查询时间
     * @return 收发料统计
     */
    @GetMapping("/materiel/count")
    public Result<MaterielCountDto> materielCount(String weightType, String baseCode, String type, String time) {
        // 参数校验
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx("基地编号不可为空");
        }
        if(ObjectUtils.isEmpty(weightType)) {
            weightType = WeightTypeEnum.RECEIVE.getValue();
        } else if("send".equals(weightType)) {
            weightType = WeightTypeEnum.SEND.getValue();
        } else {
            weightType = WeightTypeEnum.RECEIVE.getValue();
        }

        // 处理查询时间
        type = ObjectUtils.isEmpty(type) ? "year" : time;
        String pattern;
        if ("day".equals(type)) {
            pattern = "yyyy-MM-dd";
        } else if ("month".equals(type)) {
            pattern = "yyyy-MM";
        } else {
            type = "year";
            pattern = "yyyy";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (ObjectUtils.isEmpty(time)) {
            time = sdf.format(new Date());
        } else {
            try {
                sdf.parse(time);
            } catch (ParseException e) {
                time = sdf.format(new Date());
            }
        }

        // 获取数据
        MaterielCountDto data = zhjdHomeV2Service.materielCount(baseCode, weightType, type, time);
        return ResultUtils.ok(data);
    }

    /**
     * @Title envLatest
     * @Description 获取最新环保数据
     * @param baseCode 基地编号
     * @return 环保数据
     */
    @GetMapping("/env/latest")
    public Result<EnvDataDto> envLatest(String baseCode) {
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx("基地编号不可为空");
        }

        EnvDataDto data = zhjdHomeV2Service.envLatest(baseCode);
        return ResultUtils.ok(data);
    }

}
